package com.tuzhi.sys.jpa.repository;

import com.tuzhi.base.jpa.repository.BaseRepo;
import com.tuzhi.sys.jpa.entity.SysRoleEntity;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

public interface SysRoleRepo extends BaseRepo<SysRoleEntity, Long> {

    @Query(value = "select r.name , r.priority from sys_role r " +
            "left join sys_user_role ur on r.id = ur.role_id " +
            "where ur.user_id = ?1 order by r.priority asc limit 1"
            ,countQuery = "select count(r.id) from sys_role r " +
            "left join sys_user_role ur on r.id = ur.role_id " +
            "where ur.user_id = ?1 order by r.priority asc limit 1"
            , nativeQuery = true)
    Map getMaxPriorityRole(Long userId);

    @Query(value = "select r.name from sys_role r " +
            "left join sys_user_role ur on r.id = ur.role_id " +
            "where ur.user_id = ?1 "
            ,countQuery = "select count(r.id) from sys_role r " +
            "left join sys_user_role ur on r.id = ur.role_id " +
            "where ur.user_id = ?1"
            , nativeQuery = true)
    List getRoleNamesByUserId(Long userId);
}
